#include "settingwithxml.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

bool SettingWithXML::readSettings()
{
    boost::property_tree::ptree tree;

    try{
        // Open and parse xml file
        boost::property_tree::xml_parser::read_xml(m_sf, tree);

        // TODO:
        // Read setting data from file

        // Server settings
        workThreadCount = tree.get< unsigned int >("setting.server.threadCount", 16);

        // Net settings
        listenHost = tree.get< std::string >("setting.net.listenHost", "any");
        listenPort = tree.get< unsigned short >("setting.net.listenPort", 19000);

        // Table settings
        chairCount = tree.get< unsigned short >("setting.table.chairCount", 6);
        logicPath = tree.get< std::string >("setting.table.logic", "");

        // Database settings
        dbHost = tree.get< std::string >("setting.db.host", "");
        if(dbHost.empty()){
            LOG_ERROR() << "Database host is empty!";
            return false;
        }

        dbUser = tree.get< std::string >("setting.db.user", "");

        if(dbUser.empty()){
            LOG_ERROR() << "Database login user can not be null.";
            return false;
        }

        dbPassword = tree.get< std::string >("setting.db.password", "");

        if(dbPassword.empty()){
            LOG_ERROR() << "Database login password can not be null.";
            return false;
        }

        dbName = tree.get< std::string >("setting.db.db", "");

        if(dbName.empty()){
            LOG_ERROR() << "Need specify witch database to use.";
            return false;
        }

        dbPort = tree.get< unsigned short >("setting.db.port", 3306);
    }catch(boost::property_tree::xml_parser::xml_parser_error &e){
        LOG_ERROR() << e.message();
        return false;
    }

    return true;
}
